#ifndef __WLAN_H__
#define __WLAN_H__

#ifdef __cplusplus
extern "C" {
#endif

#include "iosdrv.h"

/* WLAN device modes */
typedef enum {
    WLAN_MODE_NONE = 0,    /* Stop working mode */
    WLAN_MODE_STATION,     /* Station mode */
    WLAN_MODE_AP,         /* Access Point mode */
    WLAN_MODE_MAX         /* Invalid mode */
} WlanMode_t;

typedef struct Wlan Wlan;



typedef struct {
    uint8_t bssid[6];      /**< MAC address of the AP */
    uint8_t ssid[33];      /**< SSID of the AP */
    uint8_t channel;       /**< Channel of the AP */
} WlanScanInfo;

typedef struct {
    WlanScanInfo* scan_results;
    int scan_count;
    int is_scanning;
} WlanConfig;

/* Network connection status and information */
typedef struct {
    uint8_t connected;          /* Connection status: 1-connected, 0-disconnected */
    uint8_t ip_addr[4];        /* IP address */
    uint8_t gateway[4];        /* Gateway address */
    uint8_t netmask[4];        /* Subnet mask */
    uint8_t dns1[4];           /* Primary DNS server */
    uint8_t dns2[4];           /* Secondary DNS server */
    uint8_t mac[6];            /* MAC address */
    char ssid[33];             /* Connected SSID */
    uint8_t channel;           /* Current channel */
    int8_t rssi;               /* Signal strength */
} WlanNetInfo;

/* WLAN configuration storage structure */
typedef struct {
    char ssid[33];        // SSID
    char password[65];    // Password
    uint32_t crc;        // CRC校验
} WlanStorageConfig;

/* E2P storage address definitions */
#define WLAN_CONFIG_ADDR     0   // E2P存储地址
#define WLAN_CONFIG_SIZE     sizeof(WlanStorageConfig)


/* WLAN device operations */
typedef struct WlanOps {
    int (*set_mode)(Wlan *dev, WlanMode_t mode);
    WlanMode_t (*get_mode)(Wlan *dev);
    int (*scan)(Wlan *dev, WlanScanInfo *info,  int max_results);
    int (*connect)(Wlan *dev, const char *ssid, const char *password, uint32_t timeout_sec);
    int (*startap)(Wlan *dev, const char *ssid, const char *password);
    int (*disconnect)(Wlan *dev);
    int (*get_net_info)(Wlan *dev, WlanNetInfo *info);
    int (*read_config)(Wlan *dev, char *ssid, char *password);
    int (*save_config)(Wlan *dev, const char *ssid, const char *password);
    int (*get_mac)(Wlan *dev, uint8_t *mac);
} WlanOps;

/* WLAN device structure */
typedef struct Wlan {
    IOSDrv super;            /* Inherit from Device_t */
    struct {
        WlanMode_t mode;       /* Current working mode */
        WlanConfig *config;
        const WlanOps *ops;    /* WLAN operations */
    } Protected;
} Wlan;


/* API functions */
void WlanCreate(Wlan *dev, const char *name, const WlanOps *ops);
int WlanSetMode(Wlan *dev, WlanMode_t mode);
WlanMode_t WlanGetMode(Wlan *dev);
int WlanScan(Wlan *dev, WlanScanInfo *info, int max_results);
int WlanStartAP(Wlan *dev, const char *ssid, const char *password);
int WlanConnect(Wlan *dev, const char *ssid, const char *password, uint32_t timeout_sec);
int WlanDisconnect(Wlan *dev);
int WlanGetNetInfo(Wlan *dev, WlanNetInfo *info);
int WlanSmartConfig(Wlan *dev, const char *name, uint32_t timeout_sec);

#ifdef __cplusplus
}
#endif

#endif /* __WLAN_H__ */ 